function parada(){
	delete row;
	row = new Array();
	pBox = new Array();
	irow = 0;
	ndb = 0;
	setTimeout('queue()',50);
}

function bgPeer(id){
	if(id != null){
		switch(document.getElementById(id).className){
			case "rpeer":
				document.getElementById(id).className = "peer"
			break;
			case "peer":
				document.getElementById(id).className = "rpeer"
			break;
		}
	}
}

function rPeer(id,act){
	var res = false;
	var loc = 0;
	if(pBox.length>0){
		for(i=0;i<pBox.length;i++){
			if(pBox[i] == id){
				res = true;
				loc = i;
			}
		}
	}
	if(act == "add" && res == false){ pBox.push(id);document.getElementById("peerEImage"+id).style.display = "block";document.getElementById("peerEImage"+id).src = "inc/img/lineIn.gif"}
	if(act == "del" && res == true){ pBox.splice(loc,1);}
}

function tBlink(){
	switch(window.ndb){
		case 0:
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
			for(i=0;i<pBox.length;i++){
				bgPeer(pBox[i]);
			}
			window.ndb++
			break;
		case 6:
		case 7:
		case 8:
			window.ndb++
			break;
		default:
			window.ndb = 0;
	}
}

function sBlink(){
	tBlink();
	mB = setTimeout("sBlink()",100);
}

function ajaxHTML(act,url){
    row[row.length]=[act,url];
    if((irow+1)==row.length)ajaxRun()
}

function ajaxRun(){
	var xmlHttp;
	try{ xmlHttp=new XMLHttpRequest(); }
	catch (e){
		try{ xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
	catch (e){
		try{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
		catch (e){ alert("No AJAX!?"); return false; } 
	}}
	xmlHttp.open("GET",row[irow][1]+"&uncache="+new Date().getTime(),true);
	xmlHttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
	xmlHttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
	xmlHttp.setRequestHeader("Pragma", "no-cache");
	xmlHttp.onreadystatechange=function() {
		if (xmlHttp.readyState==4){
			trataLogin(row[irow][0],unescape(xmlHttp.responseText.replace('/+/g'," ")));
			irow++;
			if(irow<row.length)ajaxRun();
		}
	}
	xmlHttp.send(null);
}

function trataLogin(act,dados){
	switch(act){
		case "loadPeers":
			var peers = dados.split(";");
			allPeers = new Array();
			for(i=1;i<(peers.length-1);i++){
				allPeers.push(peers[i].split(","));
			}
			window.totalPeers = peers[0];
			organize(window.innerWidth,window.innerHeight);
		break;
		case "Core":
			var ck = dados.split(";");
			document.cookie = 'apmscid='+ck[0]+';expires='+ck[2]+';path=/';
			document.cookie = 'apmsadm='+ck[1]+';expires='+ck[2]+';path=/';
		break;
		case "Monitor":
			var peers = dados.split(";");
			if (window.totalPeers != peers[0]){
				allPeers = new Array();
				for(i=1;i<(peers.length-1);i++){
					allPeers.push(peers[i].split(","));
				}
				window.totalPeers = peers[0];
				organize(window.innerWidth,window.innerHeight);
			} else {
				for(i=1;i<(peers.length-1);i++){
					peers2 = peers[i].split(",");
					if (document.getElementById("CallerID"+peers2[0]+"").innerHTML != "&nbsp;"+peers2[1]+"&nbsp;") {
						document.getElementById("CallerID"+peers2[0]+"").innerHTML = "&nbsp;"+peers2[1]+"&nbsp;";
					}
					if (peers2[2] != null) {
						channel = peers2[2].split("|");
						if (document.getElementById("Extension"+peers2[0]+"").innerHTML != channel[1]){
							document.getElementById("Extension"+peers2[0]+"").innerHTML = channel[1];
						}
						switch(channel[0]){
							case "rIn":
								rPeer(peers2[0],"add");
							break;
							case "rOut":
								if(document.getElementById(peers2[0]).className != "ropeer"){
									document.getElementById(peers2[0]).className = "ropeer"
									document.getElementById("peerEImage"+peers2[0]).style.display = "block";
									document.getElementById("peerEImage"+peers2[0]).src = "inc/img/lineOut.gif";
								}
							break;
							case "In":
								if(document.getElementById(peers2[0]).className != "cpeer"){
									document.getElementById(peers2[0]).className = "cpeer";
									document.getElementById("peerEImage"+peers2[0]).style.display = "block";
									document.getElementById("peerEImage"+peers2[0]).src = "inc/img/lineIn.gif";
								}
							break;
							case "Out":
								if(document.getElementById(peers2[0]).className != "cpeer"){
									document.getElementById(peers2[0]).className = "cpeer";
									document.getElementById("peerEImage"+peers2[0]).style.display = "block";
									document.getElementById("peerEImage"+peers2[0]).src = "inc/img/lineOut.gif";
								}
							break;
						}
						document.getElementById("Time"+peers2[0]+"").innerHTML = channel[4];
					} else {
						if (document.getElementById("Extension"+peers2[0]+"").innerHTML != "&nbsp;"){
							rPeer(peers2[0],"del");
							document.getElementById(peers2[0]).className = "peer"
							document.getElementById("peerEImage"+peers2[0]).style.display = "none";
							document.getElementById("Extension"+peers2[0]+"").innerHTML = "&nbsp;";
							document.getElementById("Time"+peers2[0]+"").innerHTML = "&nbsp;";
						}
					}
				}
			}
		break;
		case "Queue":
			var queue = dados.split(";");
			document.getElementById("tQc").innerHTML = queue[0];
			if(queue[0] >= 10){
				for(i=1;i<11;i++){
					queue2 = queue[i].split(",");
					document.getElementById("qC"+i).innerHTML = queue2[0];
					document.getElementById("qT"+i).innerHTML = queue2[1];
				}
			}
			if(queue[0] <= 9){
				for(i=1;i<(queue.length-1);i++){
					queue2 = queue[i].split(",");
					document.getElementById("qC"+i).innerHTML = queue2[0];
					document.getElementById("qT"+i).innerHTML = queue2[1];
				}
				for(i=(queue.length-1);i<11;i++){
					if(document.getElementById("qC"+i).innerHTML != "&nbsp;"){
						document.getElementById("qC"+i).innerHTML = "&nbsp;";
						document.getElementById("qT"+i).innerHTML = "&nbsp;";
					}
				}
			}
		break;
		case "QueuePeer":
			var queue = dados.split(";");
			if(queue.length > 1){
				var queue2 = queue[1].split(",");
				if (queue2.length < 3){
					if (document.getElementById("MainPeer").className != "MainPeer"){
						document.getElementById("MainPeer").className = "MainPeer"
					}
					switch (queue[0]){
						case "name":
							if (document.getElementById("MainPeer").innerHTML != queue2[1]){
								document.getElementById("MainPeer").innerHTML = queue2[1];
							}
						break;
						case "number":
							if (document.getElementById("MainPeer").innerHTML != queue2[0]){
								document.getElementById("MainPeer").innerHTML = queue2[0];
							}
						break;
					}
				} else {
					channel = queue2[2].split("|");
					if (document.getElementById("MainPeer").innerHTML != channel[1]){
						if (document.getElementById("MainPeer").className != "rMainPeer"){
							document.getElementById("MainPeer").className = "rMainPeer"
						}
						document.getElementById("MainPeer").innerHTML = channel[1];
					}
				}
			} else {
				if (document.getElementById("MainPeer").innerHTML != "offline"){
					document.getElementById("MainPeer").style.fontStyle = "italic";
					document.getElementById("MainPeer").style.fontWeight = "bold";
					document.getElementById("MainPeer").innerHTML = "offline";
				}
			}
		break;
	}
}

function queue(){
	trataLogin("loadPeers",'0');
	sBlink();
	setTimeout("monitor()",1000);
}

function monitor(){
	ajaxHTML("Core",'/core/');
	ajaxHTML("Queue",'/queue/');
	ajaxHTML("QueuePeer",'/queuepeer/');
	ajaxHTML("Monitor",'/monitor/');
	setTimeout("monitor()",1000);
}